package com.lungle.warmdiary.app.view.testcampaign.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.response.AppSummary;
import com.lungle.warmdiary.app.model.response.CampaignComment;
import com.lungle.warmdiary.app.model.response.CampaignMycomment;
import com.lungle.warmdiary.app.ui.widget.AutoGridView;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.lungle.warmdiary.app.view.testcampaign.CampaignDetailListTab;
import com.lungle.warmdiary.app.view.testcampaign.fragment.CampaignCommentFragment;
import com.lungle.warmdiary.app.view.testcampaign.fragment.rate.CampaignListItem1;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhangxueyuan on 16/10/3.
 * <p>
 * 亲测详情-旁边的亲测  list adapter
 */
public class CampaignDetailListAdapter extends ListBaseAdapter<CampaignListItem1> {

    ImageLoader imageLoader;
    private CampaignGridShowAdapter mAdapter;

    private static final int CONTENT_MAX = 200;

    public CampaignDetailListAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mDatas != null && mDatas.size() == 1)
            setLoadFinishText(R.string.loading_no_more2);
        return super.getView(position, convertView, parent);
    }

    // 每个convert view都会调用此方法，获得当前所需要的view样式
    @Override
    public int getItemViewType(int position) {

        if (position >= mDatas.size())
            return -1;
        return mDatas.get(position).getTYPE();
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    protected View getRealView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        ViewHolder1 vh1 = null;
        ViewHolder2 vh2 = null;

        int type = getItemViewType(position);

        imageLoader = ImageLoader.getInstance();
        if (convertView == null || convertView.getTag() == null) {

            switch (type) {
                case 0:
                    convertView = getLayoutInflater(parent.getContext()).inflate(
                            R.layout.list_cell_list_detail_campaign, null);
                    vh = new ViewHolder(convertView);
                    convertView.setTag(vh);
                    break;
                case 1:
                    convertView = getLayoutInflater(parent.getContext()).inflate(
                            R.layout.list_cell_list_detail_app, null);
                    vh1 = new ViewHolder1(convertView);
                    convertView.setTag(vh1);
                    break;
                case 2:
                    convertView = getLayoutInflater(parent.getContext()).inflate(
                            R.layout.list_cell_list_detail_my_campaign, null);
                    vh2 = new ViewHolder2(convertView);
                    convertView.setTag(vh2);
                    break;
            }

        } else {
            switch (type) {
                case 0:
                    vh = (ViewHolder) convertView.getTag();
                    break;
                case 1:
                    vh1 = (ViewHolder1) convertView.getTag();
                    break;
                case 2:
                    vh2 = (ViewHolder2) convertView.getTag();
                    break;
            }
        }
        final CampaignListItem1 item1 = (CampaignListItem1) mDatas.get(position);


        switch (type) {

            case 0:
                final CampaignComment item = item1.getCampaignComment();
                vh.rateItemName.setText(item.getUser());
                if (item.getTester() == 1)
                    vh.rateItemLevel.setText("(亲测师)");
                else
                    vh.rateItemLevel.setText(String.format("(%s)", item.getLevel()));

                if (item.getRate() == 0) {
                    vh.rateItemRatingbar.setVisibility(View.GONE);
                    vh.rateItemRatingtext.setVisibility(View.GONE);
                } else {
                    vh.rateItemRatingbar.setVisibility(View.VISIBLE);
                    vh.rateItemRatingtext.setVisibility(View.VISIBLE);
                }

                vh.rateItemRatingbar.setRating(item.getRate());
                vh.rateItemRatingtext.setText(CampaignDetailListTab.getTitleByRate(item.getRate()));
                vh.rateItemRatingtext.setTextColor(CampaignDetailListTab.getTabByRate(item.getRate()).color);

                String content = item.getContent();
                if (!TextUtils.isEmpty(content) && content.length() > CONTENT_MAX) {
                    content = content.substring(0, CONTENT_MAX);
                    vh.rateItemContent.setText(String.format("%s...", content));
                } else {
                    vh.rateItemContent.setText(content);
                }

                mAdapter = new CampaignGridShowAdapter(mContext, item.getImages(), 5);
                vh.rateItemImages.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();

                break;
            case 1:
                final AppSummary appSummary = item1.getSummary();
                imageLoader.displayImage(appSummary.getIcon(), vh1.campaignIcon);
                vh1.campaignTitle.setText(String.format("%s | %s", appSummary.getName(), appSummary.getBrief()));

//                vh1.campaignTitle.setText(appSummary.getTitle());
                if (appSummary.getRate() == 0) {
                    vh1.rateRatingbar.setVisibility(View.GONE);
                    vh1.rateRatingtext.setVisibility(View.GONE);
                    vh1.totaltext.setVisibility(View.GONE);
                } else {
                    vh1.rateRatingbar.setVisibility(View.VISIBLE);
                    vh1.rateRatingtext.setVisibility(View.VISIBLE);
                    vh1.totaltext.setVisibility(View.VISIBLE);

                }

                vh1.rateRatingbar.setRating(appSummary.getRate());
                vh1.rateRatingtext.setText(CampaignDetailListTab.getTitleByRate(appSummary.getRate()));
                vh1.rateRatingtext.setTextColor(CampaignDetailListTab.getTabByRate(appSummary.getRate()).color);

                break;

            case 2:
                final CampaignMycomment comment = item1.getCampaignMycomment();

                if (comment.getStatus() == 2 || comment.getStatus() == 1) {
                    vh2.imageView.setVisibility(View.VISIBLE);
                } else {
                    vh2.imageView.setVisibility(View.GONE);
                }


                if (comment.getRate() == 0) {
                    vh2.rateItemRatingbar.setVisibility(View.GONE);
                    vh2.rateItemRatingtext.setVisibility(View.GONE);


                } else {
                    vh2.rateItemRatingbar.setVisibility(View.VISIBLE);
                    vh2.rateItemRatingtext.setVisibility(View.VISIBLE);
                }

                vh2.rateItemRatingbar.setRating(comment.getRate());
                vh2.rateItemRatingtext.setText(CampaignDetailListTab.getTitleByRate(comment.getRate()));
                vh2.rateItemRatingtext.setTextColor(CampaignDetailListTab.getTabByRate(comment.getRate()).color);
                vh2.rateItemStatus.setText(CampaignMycomment.Status.getTabByIdx(comment.getStatus()).title);
                vh2.rateItemStatus.setTextColor(CampaignMycomment.Status.getTabByIdx(comment.getStatus()).color);

                String content1 = comment.getContent();
                if (!TextUtils.isEmpty(content1) && content1.length() > CONTENT_MAX) {
                    content1 = content1.substring(0, CONTENT_MAX);
                    vh2.rateItemContent.setText(String.format("%s...", content1));
                } else {
                    vh2.rateItemContent.setText(content1);

                }


                mAdapter = new CampaignGridShowAdapter(mContext, comment.getImages(), 5);
                vh2.rateItemImages.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();

//                if (comment != null && comment.getAuditInfo() != null) {
                if (comment != null && comment.getStatus() == 2) {

                    convertView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            UIHelper.showCampaignComment(mContext, comment, CampaignCommentFragment.UPDATE);
                        }
                    });
                } else {

                    convertView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            UIHelper.showCampaignCommentDetail(mContext, comment);
                        }
                    });
                }
                break;
        }
        return convertView;
    }

    static class ViewHolder {

        @Bind(R.id.rate_item_name)
        TextView rateItemName;
        @Bind(R.id.rate_item_level)
        TextView rateItemLevel;
        @Bind(R.id.rate_item_ratingbar)
        RatingBar rateItemRatingbar;
        @Bind(R.id.rate_item_ratingtext)
        TextView rateItemRatingtext;
        @Bind(R.id.rate_item_content)
        TextView rateItemContent;
        @Bind(R.id.rate_item_images)
        AutoGridView rateItemImages;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    static class ViewHolder1 {

        @Bind(R.id.campaign_icon)
        ImageView campaignIcon;
        @Bind(R.id.campaign_title)
        TextView campaignTitle;
        @Bind(R.id.rate_ratingbar)
        RatingBar rateRatingbar;
        @Bind(R.id.rate_ratingtext)
        TextView rateRatingtext;
        @Bind(R.id.campaign_app)
        LinearLayout campaignApp;
        @Bind(R.id.totaltext)
        TextView totaltext;

        public ViewHolder1(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder2 {

        @Bind(R.id.rate_item_text)
        TextView rateItemText;
        @Bind(R.id.rate_item_status)
        TextView rateItemStatus;
        @Bind(R.id.imageView)
        ImageView imageView;
        @Bind(R.id.campaign_rate_status)
        RelativeLayout campaignRateStatus;
        @Bind(R.id.rate_item_ratingbar)
        RatingBar rateItemRatingbar;
        @Bind(R.id.rate_item_ratingtext)
        TextView rateItemRatingtext;
        @Bind(R.id.rate_item_content)
        TextView rateItemContent;
        @Bind(R.id.rate_item_images)
        AutoGridView rateItemImages;
        @Bind(R.id.campaign_rate_mycomment)
        LinearLayout campaignRateMycomment;

        public ViewHolder2(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
